﻿using System;
using System.Linq.Expressions;
using System.Web.UI;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.En.ContentDL
{
	public partial class ComDetail : BasePage
	{
		protected string Con { get; set; }
		protected string CTitle { get; set; }
		protected string DateP { get; set; }
		protected string CountR { get; set; }
		protected string Img { get; set; }
		private int NewsID
		{
			get;
			set;
		}
		protected void Page_Load(object sender, EventArgs e)
		{
			ImgData();
			if (!Page.IsPostBack)
			{
				int id = 0;
				if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
				{
					this.NewsID = id;
				}
				BindData(this.NewsID);

				news_subtitle.Visible = string.IsNullOrWhiteSpace(Request.QueryString["nc"]);
			}
		}

		protected void BindData(int id)
		{

			Expression<Func<TB_News, bool>> f;
			if (id <= 0)
			{
				f = (p => p.Type == this.SystemCode && p.IsEn);
			}
			else
			{
				f = (p => p.ID == id);
			}
			var resultQ = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_News>(f);
			// 是否有结果
			if (resultQ != null)
			{
				this.linkAttarchment.HRef = resultQ.AttachmentsUrl;
				this.linkAttarchment.Visible = !String.IsNullOrWhiteSpace(resultQ.AttachmentsUrl);
				this.CTitle = resultQ.Title.ToString();
				this.DateP = resultQ.CreateDate.ToString("yyyy-MM-dd");
				this.CountR = resultQ.ReadCount.ToString();
				if (resultQ.Content != "")
				{
					this.Con = resultQ.Content.ToString();
				}
				else
				{
					this.Con = resultQ.Summary.ToString();
				}

				resultQ.ReadCount = resultQ.ReadCount + 1;
				DAL.DALHelper.Instance.Update<TB_News>(resultQ);
				DAL.DALHelper.Instance.SaveChanges();
			}
		}

		protected void ImgData()
		{
			string type;
			if (string.IsNullOrWhiteSpace(Request.QueryString["st"]))
			{
				type = "";
			}
			else
			{
				type = Request.QueryString["st"];
			}
			Expression<Func<TB_SystemConfig, bool>> f = (p => p.Code == "EnImg" && p.Type == type);
			var resultQ = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_SystemConfig>(f);
			if (resultQ != null)
			{
				this.Img = resultQ.Value.ToString();
			}
		}
	}
}